package cn.wolfcode.web.controller;

import cn.wolfcode.transfer.domain.Department;
import cn.wolfcode.transfer.qo.PageResult;
import cn.wolfcode.transfer.qo.QueryObject;
import cn.wolfcode.transfer.service.IDepartmentService;
import cn.wolfcode.transfer.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @RequiredPermission(name = "部门页面", expression = "department:list")
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo) {
        PageInfo<Department> pageInfo = departmentService.getPageInfo(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "department/list";

    }

    @RequiredPermission(name = "部门删除", expression = "department:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            departmentService.deleteByPrimaryKey(id);
        }
        return "redirect:/department/list";
    }
    @RequiredPermission(name = "部门新增/编辑", expression = "department:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Department department) {
        if (department.getId() == null) { // 新增
            departmentService.insert(department);
        } else {
            departmentService.updateByPrimaryKey(department); // 修改
        }
        return "redirect:/department/list"; // 让浏览器重新发一次请求
    }

}
